﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineStore.Bll;

namespace OnlineStore.Dal
{
    public class DalProduct
    {
        OnlineStoreDataContext data = new OnlineStoreDataContext();
        public static int PageNumber = 8;
        int _NumberItem = 0;
        public DalProduct() {
        }
        public List<tbProduct> Search( string Type,int page)
        {
            List<tbProduct> listProduct = ((from p in data.tbProducts
                                            where p.Type == Type
                                            select p).Skip(page * PageNumber).Take(PageNumber)).ToList();
            return listProduct;
        }
        public int NumberItem(string type)
        {
            int max = (from p in data.tbProducts
                       where p.Type == type
                       select p).Count();
            return max;
        }
        public int NumberItem()
        {
            int max = ((from p in data.tbProducts
                        where p.Father == null
                        select p).Take(PageNumber)).Count();
            return max;
        }
        public List<tbProduct> Search(string Ma)
        {
            List<tbProduct> listProduct = (from p in data.tbProducts
                                           where p.Product_ID == Ma
                                           select p).ToList();
            return listProduct;
        }
        public List<tbProduct> Search()
        {
            List<tbProduct> listProduct = ((from p in data.tbProducts
                                            where p.Father == null
                                            select p).Take(PageNumber)).ToList();
            return listProduct;
        }
        public List<tbProduct> Outstanding()
        {
            List<tbProduct> listProduct = ((from p in data.tbProducts
                                            where p.Father == null
                                            orderby p.Ord_Qty descending
                                            select p).Take(PageNumber)).ToList();
            return listProduct;
        }
        public tbProduct Search(int ID)
        {
            tbProduct _product = (from p in data.tbProducts
                                  where p.ID == ID
                                  select p).FirstOrDefault();
            return _product;
        }
        public List<tbProduct> Search(string keyword, string type, string Introduce, string Detail,int Page)
        {
            List<tbProduct> list = new List<tbProduct>();
            if (keyword == null) return list;
            if (Introduce != Detail)
            {
                if (Introduce != null)
                {
                    if (type == "-1")
                    {
                        _NumberItem = (from p in data.tbProducts
                                       where p.Introduce.Contains(keyword)
                                       select p).Count();
                        list = (from p in data.tbProducts
                                where p.Introduce.Contains(keyword)
                                select p).Skip(Page * PageNumber).Take(PageNumber).ToList();
                    }
                    else
                    {
                        _NumberItem = (from p in data.tbProducts
                                       where p.Introduce.Contains(keyword) && p.Type == type
                                       select p).Count();
                        list = (from p in data.tbProducts
                                where p.Introduce.Contains(keyword) && p.Type == type
                                select p).Skip(Page * PageNumber).Take(PageNumber).ToList();
                    }
                }
                else
                {
                    if (type == "-1")
                    {
                        _NumberItem = (from p in data.tbProducts
                                       where p.Introduce.Contains(keyword)
                                       select p).Count();
                        list = (from p in data.tbProducts
                                where p.Introduce.Contains(keyword)
                                select p).Skip(Page * PageNumber).Take(PageNumber).ToList();
                    }
                    else
                    {
                        _NumberItem = (from p in data.tbProducts
                                       where p.Introduce.Contains(keyword) && p.Type == type
                                       select p).Count();
                        list = (from p in data.tbProducts
                                where p.Introduce.Contains(keyword) && p.Type == type
                                select p).Skip(Page * PageNumber).Take(PageNumber).ToList();
                    }
                }
            }
            else
            {
                if (type == "-1" || type == null)
                {
                    _NumberItem = (from p in data.tbProducts
                                   where p.Introduce.Contains(keyword) || p.Introduce.Contains(keyword)
                                   select p).Count();
                    list = (from p in data.tbProducts
                            where p.Introduce.Contains(keyword) || p.Introduce.Contains(keyword)
                            select p).Skip(Page * PageNumber).Take(PageNumber).ToList();
                }
                else
                {
                    _NumberItem = (from p in data.tbProducts
                                   where (p.Introduce.Contains(keyword) || p.Introduce.Contains(keyword)) && p.Type == type
                                   select p).Count();
                    list = (from p in data.tbProducts
                            where (p.Introduce.Contains(keyword) || p.Introduce.Contains(keyword)) && p.Type == type
                            select p).Skip(Page * PageNumber).Take(PageNumber).ToList();
                }
            }
            return list;
        }
        public int NumberItems
        {
            get { return _NumberItem; }
        }
    }
}